%proto(hci,s,umc,ci,pri(lst, [pri(hash, [cmr(bsn, [u], [])]), pri((sum,all),[cm(d, [u], dot)])])).
%proto(hci,st,umc,_,pri(lst, [cmr(bsn, [u], []), pri(hash, [cmr(bsn, [u], [])])])).
%proto(hci,st,ci,_, pri(lst, [pri(hash, [cmr(bsn, [u], [])]), cm(ip, [umc], []), pri((sum,all),[cm(d, [u], dot)])])).

%instance(ctxm(alpha_1_,umc,ip, (ip,umc1), (ip,umc1))).
%instance(ctxm(alpha_1_,ci,ip, (ip,ci), (ip,ci))).
%instance(ctxm(alpha_1_,u,bsn,i_bsn,i_bsn)).
%instance(ctxm(alpha_1_,u,(d,all,1), (i,d_1), (c,d_1))).
%instance(ctxm(alpha_1_,u,(d,all,2), (i,d_2), (c,d_2))).
%instance(ctxm(alpha_1_,u,(d,all,3), (i,d_3), (c,d_3))).

%actor(ctx(alpha_1_,umc),umc1,ip).
%actor(ctx(alpha_1_,ci),ci,ip).

zipnum(_,[],[]).
zipnum(N,[X|LST],[(N,X)|LSTP]) :- NP1 is N+1, zipnum(NP1,LST,LSTP).



instancemn(cm(V,[P],_),D,FAM,N,RET) :- RET=ctxm(D,P,(V,FAM,N),_,_), instance(RET).
instancemn(cm(V,dot,_),D,FAM,N,RET) :- RET=ctxm(D,dot,(V,FAM,N),_,_), instance(RET).
instancemn(cmr(V,[P],_),D,FAM,N,RET) :- RET=ctxm(D,P,(V,FAM,N),_,_), instance(RET).
instancemn(cmr(V,dot,_),D,FAM,N,RET) :- RET=ctxm(D,dot,(V,FAM,N),_,_), instance(RET).

instancemn([],_,_,_,[]).
instancemn([X|LST],D,FAM,N,[XR|LSTR]) :- instancemn(X,D,FAM,N,XR), instancemn(LST,D,FAM,N,LSTR).
instancemn(pri(T,LST),D,FAM,N,pri(T,LSTR)) :- instancemn(LST,D,FAM,N,LSTR).

instancemns(M,D,FAM,N,[X|LST]) :-
  instancemn(M,D,FAM,N,X), !, NP1 is N+1, instancemns(M,D,FAM,NP1,LST).
instancemns(_,_,_,_,[]).

mk1([X],X) :- !.
mk1(LST,pri(lst,LST)).

instancem([],_,[]).
instancem([X|LST],D,[XR|LSTR]) :- instancem(X,D,XR), instancem(LST,D,LSTR).
instancem(cm(V,[P],_),D,[RET]) :- RET=ctxm(D,P,V,_,_), instance(RET).
instancem(cm(V,dot,_),D,[RET]) :- RET=ctxm(D,dot,V,_,_), instance(RET).
instancem(cmr(V,[P],_),D,[RET]) :- RET=ctxm(D,P,V,_,_), instance(RET).
instancem(cmr(V,dot,_),D,[RET]) :- RET=ctxm(D,dot,V,_,_), instance(RET).
instancem(pri((sum,FAM),[M]),D,RET) :- instancemns(M,D,FAM,1,RET).
instancem(pri(lst,LST),D,[pri(lst,LSTR)]) :- instancem(LST,D,LSTRP), append(LSTRP,LSTR), !.
instancem(pri(T,LST),D,[pri(T,LSTR)]) :- instancem(LST,D,LSTRP), mk1(LSTRP,LSTR).

protoinst(s,DOM,A1,A2,M,step((RA1,DOM),(RA2,DOM),tm(s,[CAX1,CAX2],RM))) :-
  actor(ctx(DOM,A1),RA1,CAI1),
  actor(ctx(DOM,A2),RA2,CAI2),
  CAX1=ctxm(DOM,A1,CAI1,CA1,CA1), instance(CAX1),
  CAX2=ctxm(DOM,A2,CAI2,CA2,CA2), instance(CAX2),
  instancem(M,DOM,RMP), mk1(RMP,RM).
  
protoinst(st,DOM,A1,_,M,step((RA1,DOM),(RA1,DOM),tm(st,RM))) :-
  actor(ctx(DOM,A1),RA1,_),
  instancem(M,DOM,RMP), mk1(RMP,RM).


symstep(RET) :-
    instpr(DOM,PROTO),
    proto(PROTO,T,A1,A2,M),
    protoinst(T,DOM,A1,A2,M,RET).
    
      